<!DOCTYPE html><html><head><title>MyBatis-Plus乐观锁</title><meta charset='utf-8'><link href='https://cdn.maxiang.io/res-min/themes/marxico.css' rel='stylesheet'><style></style></head><body><div id='preview-contents' class='note-content'>
                        
                    



<h1 id="mybatis-plus乐观锁">MyBatis-Plus乐观锁</h1>

<div><div class="toc"><div class="toc">
<ul>
<li><a href="#mybatis-plus乐观锁">MyBatis-Plus乐观锁</a><ul>
<li><ul>
<li><a href="#一主要适用场景">一、主要适用场景</a></li>
<li><a href="#二注解实体字段-version-必须要">二、注解实体字段 @Version 必须要!</a></li>
<li><a href="#三配置">三、配置</a></li>
<li><a href="#四案例">四、案例</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>

<h3 id="一主要适用场景">一、主要适用场景</h3>

<p>意图 ：</p>

<pre class="prettyprint hljs-dark"><code class="hljs"><div class="hljs-line"> 当要更新一条记录的时候，希望这条记录没有被别人更新
</div></code></pre>

<p>乐观锁实现方式：</p>

<pre class="prettyprint hljs-dark"><code class="hljs applescript"><div class="hljs-line">取出记录时，获取当前<span class="hljs-built_in">version</span>
</div><div class="hljs-line">更新时，带上这个<span class="hljs-built_in">version</span>
</div><div class="hljs-line">执行更新时， <span class="hljs-keyword">set</span> <span class="hljs-built_in">version</span> = newVersion <span class="hljs-keyword">where</span> <span class="hljs-built_in">version</span> = oldVersion
</div><div class="hljs-line">如果<span class="hljs-built_in">version</span>不对，就更新失败
</div></code></pre>

<hr>

<h3 id="二注解实体字段-version-必须要">二、注解实体字段 @Version 必须要!</h3>



<pre class="prettyprint hljs-dark"><code class="hljs aspectj"><div class="hljs-line"><span class="hljs-meta">@Version</span>
</div><div class="hljs-line"><span class="hljs-keyword">private</span> Integer version;
</div><div class="hljs-line"><wbr>
</div><div class="hljs-line">特别说明:
</div><div class="hljs-line"><wbr>
</div><div class="hljs-line">支持的数据类型只有:<span class="hljs-keyword">int</span>,Integer,<span class="hljs-keyword">long</span>,Long,Date,Timestamp,LocalDateTime
</div><div class="hljs-line">整数类型下 newVersion = oldVersion + 1
</div><div class="hljs-line">newVersion 会回写到 entity 中
</div><div class="hljs-line">仅支持 updateById(id) 与 update(entity, wrapper) 方法
</div><div class="hljs-line">在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
</div><div class="hljs-line"><wbr>
</div></code></pre>

<hr>



<h3 id="三配置">三、配置</h3>

<pre class="prettyprint hljs-dark"><code class="hljs aspectj"><div class="hljs-line">  <span class="hljs-meta">@Bean</span>
</div><div class="hljs-line">    <span class="hljs-keyword">public</span> <span class="hljs-function">OptimisticLockerInterceptor <span class="hljs-title">optimisticLoker</span><span class="hljs-params">()</span> </span>{
</div><div class="hljs-line">        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> OptimisticLockerInterceptor();
</div><div class="hljs-line">    }
</div></code></pre>



<h3 id="四案例">四、案例</h3>

<pre class="prettyprint hljs-dark"><code class="hljs pf"><div class="hljs-line"> @RequestMapping(<span class="hljs-string">"/updateById"</span>)
</div><div class="hljs-line">    public void updateById() {
</div><div class="hljs-line">        User <span class="hljs-keyword">user</span>=new User();
</div><div class="hljs-line">        <span class="hljs-keyword">user</span>.<span class="hljs-built_in">set</span>Id(<span class="hljs-number">13</span>L);
</div><div class="hljs-line">        <span class="hljs-keyword">user</span>.<span class="hljs-built_in">set</span>Name(<span class="hljs-string">"张洋"</span>);
</div><div class="hljs-line">        <span class="hljs-keyword">user</span>.<span class="hljs-built_in">set</span>Age(<span class="hljs-number">13</span>);
</div><div class="hljs-line">        <span class="hljs-keyword">user</span>.<span class="hljs-built_in">set</span>Version(<span class="hljs-number">1</span>);
</div><div class="hljs-line">        if( <span class="hljs-keyword">user</span>Mapper.updateById(<span class="hljs-keyword">user</span>)&gt;<span class="hljs-number">0</span>){
</div><div class="hljs-line">            System.err.println(<span class="hljs-string">"更新成功"</span>);
</div><div class="hljs-line">        }
</div><div class="hljs-line">        else {
</div><div class="hljs-line">            System.err.println(<span class="hljs-string">"被其他人更新"</span>);
</div><div class="hljs-line">        }
</div><div class="hljs-line">    }
</div></code></pre></div></body></html>